from typing import Dict, List, Tuple


class apartment():
    def __init__(self, name) -> None:
        self.name = name
        self.parent = None
        self.son = set()
        self.bigIssue = 0
        self.issue = 0

    def update(self, level, num):
        if level:
            self.issue += num
        else:
            self.bigIssue += num


def findApartment(apartmentDict: Dict[str, apartment], name):
    if name in apartmentDict:
        return apartmentDict[name]
    else:
        ans = apartment(name)
        apartmentDict[name] = ans
        return ans


def updateRoot(root: apartment):
    for part in root.son:
        updateRoot(part)
        root.update(1, part.issue)
        root.update(0, part.bigIssue)


[M, N] = map(int, input().split())
lines = []
for _ in range(N):
    lines.append(input())

apartmentDict: Dict[str, apartment] = {}
for i in range(N):
    elements = lines[i].split()
    name = elements[0]
    parent = elements[1]
    level = int(elements[2])
    num = int(elements[3])

    part = findApartment(apartmentDict, name)
    part.update(level, num)
    if (parent != '*'):
        parentPart = findApartment(apartmentDict, parent)
        parentPart.son.add(part)
        part.parent = parentPart

roots = []
for part in apartmentDict.values():
    if part.parent == None:
        roots.append(part)
        updateRoot(part)

cnt = 0
for part in apartmentDict.values():
    score = 5*part.bigIssue+2*part.issue
    if score > M:
        cnt += 1
print(cnt)
